package com.nh.actionPhone;

import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.nh.entity.NgUser;
import com.nh.entity.User;
import com.nh.utils.http.Information;

//userInfoAction!updata.action?phoneNum=15208712635&deviceNumber=fc65d01df7a94e93abace46df591111c

public class UserInfoAction extends BasePhoneAction {
	private String phoneNum;
	private String deviceNumber;

	/*
	 * public void updata() { try { List<User> users =
	 * this.getBaseService().findByProperty("username", deviceNumber,
	 * User.class); if (null != users && users.size() > 0) { User userTemp =
	 * users.get(0); userTemp.setName(phoneNum);
	 * this.getBaseService().saveOrUpdate(userTemp, User.class);
	 * writResponse("{'state':'success'}"); } else {
	 * writResponse("{'state':'false'}"); } } catch (Exception e) {
	 * writResponse("{'state':'false'}"); // TODO Auto-generated catch block
	 * e.printStackTrace(); }
	 * 
	 * }
	 */
	public void updata() {
		try {
			List<NgUser> users = this.getBaseService().findByProperty("phoneNum", deviceNumber, NgUser.class);
			if (null != users && users.size() > 0) {
				NgUser userTemp = users.get(0);
				// userTemp.setPhoneNum(phoneNum);
				userTemp.setDrivecode(phoneNum);
				userTemp.setRegisterTime(new Timestamp(System.currentTimeMillis()));
				this.getBaseService().saveOrUpdate(userTemp, NgUser.class);
				writResponse("{'state':'success'}");
			} else {
				// 第一次登录 调用webservice接口验证手机号是否是电信的，并存放数据库
				// 获取访问者的IP
				HttpServletRequest req = (HttpServletRequest) ServletActionContext.getRequest();
				String lastLoginIP = req.getHeader("x-forwarded-for");
				if (lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {
					lastLoginIP = req.getHeader("Proxy-Client-IP");
				}
				if (lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {
					lastLoginIP = req.getHeader("WL-Proxy-Client-IP");
				}
				if (lastLoginIP == null || lastLoginIP.length() == 0 || "unknown".equalsIgnoreCase(lastLoginIP)) {
					lastLoginIP = req.getRemoteAddr();
				}

				NgUser user = new NgUser();
				user.setIp(lastLoginIP);
				user.setPhoneNum(deviceNumber);
				user.setDrivecode(phoneNum);
				user.setDiscription("电信用户");
				user.setPhonetype("android智能手机");
				user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
				this.getBaseService().save(user);
				writResponse("{'state':'success'}");

			}
		} catch (Exception e) {
			writResponse("{'state':'false'}");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private boolean webserviceValidate(String phoneNum) {
		if (true) {
			return true;
		}
		String content = Information.getMobileNumerInfo(phoneNum);
		if (content.contains("您的号码不在中国电信移动业务服务范围内")) {
			return false;
		} else if (content.contains("您号码的归属地为")) {
			return true;
		}
		return true;
	}

	public String getPhoneNum() {
		return phoneNum;
	}

	public void setPhoneNum(String phoneNum) {
		this.phoneNum = phoneNum;
	}

	public String getDeviceNumber() {
		return deviceNumber;
	}

	public void setDeviceNumber(String deviceNumber) {
		this.deviceNumber = deviceNumber;
	}

}
